const Core = require('@alicloud/pop-core');

const rn = require('randomnumber');
const jwt = require("jsonwebtoken");

module.exports = {
    sendMsg(tel,code) {
        var client = new Core({
            accessKeyId: 'LTAI4G7eg7t45EBGNNMNGFgw',
            accessKeySecret: 'f36ZeMdCfen6wSnX0LzxgvWrvoRaKL',
            endpoint: 'https://dysmsapi.aliyuncs.com',
            apiVersion: '2017-05-25'
        });

        var params = {
            "RegionId": "cn-hangzhou",
            "PhoneNumbers": tel,
            "SignName": "QXL商城",
            "TemplateCode": "SMS_204756814",
            "TemplateParam": `{code:${"0" + code}}`
        }

        var requestOption = {
            method: 'POST'
        };

        return new Promise((resolve,reject)=>{
            client.request('SendSms', params, requestOption).then((result) => {
                console.log(JSON.stringify(result));
                resolve();
            }, (ex) => {
                console.log(ex);
                reject();
            })
        })

    },
    randomNum(n){
        return rn.generate(n);
    },
    verify(req){
        const t = req.headers.token
        return new Promise((resolve,reject)=>{
            jwt.verify(t,"qiuxiaolong", (err,data)=>{
                if(err) reject();
                resolve(data);
            })
        })
    }
}
